package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.impl.CategoryServicelmpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags="分类管理")
public class CategoryController {

    @Autowired
    private CategoryServicelmpl categoryServicelmpl;

    /**
     * 修改分类
     * @param categoryDTO
     * @return
     */
    @PutMapping()
    @ApiOperation("修改分类")
    @CacheEvict(cacheNames = "updateSort",allEntries = true)
    public Result updateSort(@RequestBody CategoryDTO categoryDTO){
        log.info("修改分类");
        categoryServicelmpl.updateSort(categoryDTO);
        return Result.success();
    }

    /**
     * 分页分类查询
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public Result<PageResult> findSort(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分类分页查询...");
        PageResult pageResult=categoryServicelmpl.findSort(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id启用禁用分类
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("根据id启用禁用分类")
    @CacheEvict(cacheNames = "startOrStop",allEntries = true)
    public Result startOrStop(@PathVariable("status") Integer status, @PathParam("id") Long id){
        log.info("根据id启用禁用分类...",id,status);
        categoryServicelmpl.startOrStop(id,status);
        return Result.success();
    }

    /**
     * 新增分类
     * @param categoryDTO
     * @return
     */
    @PostMapping()
    @ApiOperation("新增分类")
    @CacheEvict(cacheNames = "addSoft",key="#categoryDTO.id")
    public  Result addSoft(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类...");
        categoryServicelmpl.addSoft(categoryDTO);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    @CacheEvict(cacheNames = "findBySort",key="#type")
    public Result<List<Category>> findBySort(Integer type){
        List<Category> list = categoryServicelmpl.findBySort(type);
        return Result.success(list);
    }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @DeleteMapping()
    @ApiOperation("根据id删除分类")
    @CacheEvict(cacheNames = "deleteById",key="#id")
    public Result deleteById(String id){
        log.info("根据id删除分类...",id);
        //调用Service层
        categoryServicelmpl.deleteById(id);
        return Result.success();
    }
}
